package com.xiaochong.bitong.job.schedule;

import com.alibaba.fastjson.JSON;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.bitong.job.config.ExecutorConfig;
import com.xiaochong.bitong.job.service.IndustryService;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.mapper.CurrencyIndustryCategoryMapper;
import com.xiaochong.tir.common.data.po.CurrencyIndustryCategory;
import com.xiaochong.tir.common.data.vo.IndustryDataVo;
import com.xiaochong.tir.common.exception.hbase.HbaseCloseExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseConnectionExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseGetTableExcetion;
import com.xiaochong.tir.common.util.RedisKeyApp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Project : xc-tir
 * @Package Name : com.xiaochong.bitong.job.schedule
 * @Description : TODO
 * @Author : daizhiqing@xiaochong.com
 * @Creation Date : 2018年08月13日下午7:44
 * @ModificationHistory Who When What
 */
@Component
public class IndustryScheduled {

    @Autowired
    RedisServer redisServer;

    @Autowired
    IndustryService industryService;

    @Autowired
    CurrencyIndustryCategoryMapper currencyIndustryCategoryMapper;

    /**
     *  行业指数一年数据图表（每天零点一条）
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0 36 0 * * ?")
    public void IndustryDataVoYear(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("IndustryDataVoYear" , "IndustryDataVoYear开始" , System.currentTimeMillis()+"");

            CurrencyIndustryCategory currencyIndustryCategory = new CurrencyIndustryCategory();
            currencyIndustryCategory.setEnableFlag( true );
            currencyIndustryCategory.setDeleteFlag( false );
            currencyIndustryCategory.setParentId( 0 );
            List<CurrencyIndustryCategory> conceptCategoryList = currencyIndustryCategoryMapper.selectByEntityList( currencyIndustryCategory );
            for(int i=0;i<conceptCategoryList.size();i++){
                List list=new ArrayList();
                currencyIndustryCategory = conceptCategoryList.get( i );
                IndustryDataVo industryDataVo = industryService.getConceptualQuotesOfYear(currencyIndustryCategory.getId());
                list.add( industryDataVo );
                if(industryDataVo != null){
                    redisServer.set( RedisKeyApp.INDUSTRY_INDEX+365+":"+24+":"+currencyIndustryCategory.getId(),JSON.toJSONString( list ));
                }
            }
            LogTrace.info("IndustryDataVoYear" , "IndustryDataVoYear结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }


    /**
     *  行业指数30天数据图表（每4小时一条）
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0 33 0/4 * * ?")
    public void IndustryDataVo30Days(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("IndustryDataVo30Days" , "IndustryDataVo30Days开始" , System.currentTimeMillis()+"");

            CurrencyIndustryCategory currencyIndustryCategory = new CurrencyIndustryCategory();
            currencyIndustryCategory.setEnableFlag( true );
            currencyIndustryCategory.setDeleteFlag( false );
            currencyIndustryCategory.setParentId( 0 );
            List<CurrencyIndustryCategory> conceptCategoryList = currencyIndustryCategoryMapper.selectByEntityList( currencyIndustryCategory );
            for(int i=0;i<conceptCategoryList.size();i++){
                List list=new ArrayList();
                currencyIndustryCategory = conceptCategoryList.get( i );
                IndustryDataVo industryDataVo = industryService.getConceptualQuotesOf30Days(currencyIndustryCategory.getId());
                list.add( industryDataVo );
                if(industryDataVo != null){
                    redisServer.set( RedisKeyApp.INDUSTRY_INDEX+30+":"+24+":"+currencyIndustryCategory.getId(),JSON.toJSONString( list ));
                }
            }
            LogTrace.info("IndustryDataVo30Days" , "IndustryDataVo30Days结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }


    /**
     *  行业指数7天数据图表（每小时一条）
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0 35 0/1 * * ?")
    public void IndustryDataVo7Days(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("IndustryDataVo7Days" , "IndustryDataVo7Days开始" , System.currentTimeMillis()+"");

            CurrencyIndustryCategory currencyIndustryCategory = new CurrencyIndustryCategory();
            currencyIndustryCategory.setEnableFlag( true );
            currencyIndustryCategory.setDeleteFlag( false );
            currencyIndustryCategory.setParentId( 0 );
            List<CurrencyIndustryCategory> conceptCategoryList = currencyIndustryCategoryMapper.selectByEntityList( currencyIndustryCategory );
            for(int i=0;i<conceptCategoryList.size();i++){
                List list=new ArrayList();
                currencyIndustryCategory = conceptCategoryList.get( i );
                IndustryDataVo industryDataVo = industryService.getConceptualQuotesOf7Days(currencyIndustryCategory.getId());
                list.add( industryDataVo );
                if(industryDataVo != null){
                    redisServer.set( RedisKeyApp.INDUSTRY_INDEX+7+":"+24+":"+currencyIndustryCategory.getId(),JSON.toJSONString( list ));
                }
            }
            LogTrace.info("IndustryDataVo7Days" , "IndustryDataVo7Days结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }

    /**
     *  行业指数1天数据图表（每十分钟一条）
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="30 0/10 * * * ?")
    public void IndustryDataVoOneDays(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("IndustryDataVoOneDays" , "IndustryDataVoOneDays开始" , System.currentTimeMillis()+"");

            CurrencyIndustryCategory currencyIndustryCategory = new CurrencyIndustryCategory();
            currencyIndustryCategory.setEnableFlag( true );
            currencyIndustryCategory.setDeleteFlag( false );
            currencyIndustryCategory.setParentId( 0 );
            List<CurrencyIndustryCategory> conceptCategoryList = currencyIndustryCategoryMapper.selectByEntityList( currencyIndustryCategory );
            for(int i=0;i<conceptCategoryList.size();i++){
                List list=new ArrayList();
                currencyIndustryCategory = conceptCategoryList.get( i );
                IndustryDataVo industryDataVo = industryService.getConceptualQuotesOfOneDays(currencyIndustryCategory.getId());
                list.add( industryDataVo );
                if(industryDataVo != null){
                    redisServer.set( RedisKeyApp.INDUSTRY_INDEX+1+":"+24+":"+currencyIndustryCategory.getId(),JSON.toJSONString( list ));
                }
            }
            LogTrace.info("IndustryDataVoOneDays" , "IndustryDataVoOneDays结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }


    /**
     *  行业指数3小时数据图表（每5分钟一条）
     */
    @Async(ExecutorConfig.SCHEDULER_EXECUTOR_POOL)
    @Scheduled(cron="0 */5 * * * ?")
    public void IndustryDataVo3Hours(){
        try {
            LogTrace.beginTrace();
            LogTrace.info("IndustryDataVo3Hours" , "IndustryDataVo3Hours开始" , System.currentTimeMillis()+"");

            CurrencyIndustryCategory currencyIndustryCategory = new CurrencyIndustryCategory();
            currencyIndustryCategory.setEnableFlag( true );
            currencyIndustryCategory.setDeleteFlag( false );
            currencyIndustryCategory.setParentId( 0 );
            List<CurrencyIndustryCategory> conceptCategoryList = currencyIndustryCategoryMapper.selectByEntityList( currencyIndustryCategory );
            for(int i=0;i<conceptCategoryList.size();i++){
                List list=new ArrayList();
                currencyIndustryCategory = conceptCategoryList.get( i );
                IndustryDataVo industryDataVo = industryService.IndustryDataVo3Hours(currencyIndustryCategory.getId());
                list.add( industryDataVo );
                if(industryDataVo != null){
                    redisServer.set( RedisKeyApp.INDUSTRY_INDEX+1+":"+3+":"+currencyIndustryCategory.getId(),JSON.toJSONString( list ));
                }
            }
            LogTrace.info("IndustryDataVo3Hours" , "IndustryDataVo3Hours结束" , System.currentTimeMillis()+"");
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
        }catch (Exception e){
            LogTrace.error(e.getMessage() ,e);
            e.printStackTrace();
        }finally {
            LogTrace.endTrace();
        }
    }
}
